package com.quartet.modules.vehicle.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.quartet.modules.vehicle.dao.TOilConsumptionEquipmentDao;
import com.quartet.modules.vehicle.dto.TOilConsumptionEquipmentAddRequestDto;
import com.quartet.modules.vehicle.entity.TOilConsumptionEquipment;
import com.quartet.modules.vehicle.service.TOilConsumptionEquipmentService;
import com.quartet.utils.JsonTool;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.List;

/**
 * 油耗设置信息表(TOilConsumptionEquipment)表服务实现类
 *
 * @author makejava
 * @since 2021-05-04 16:39:35
 */
@Service("tOilConsumptionEquipmentService")
public class TOilConsumptionEquipmentServiceImpl extends ServiceImpl<TOilConsumptionEquipmentDao, TOilConsumptionEquipment> implements TOilConsumptionEquipmentService {

    /**
     * 添加油耗设置信息
     *
     * @param tOilConsumptionEquipmentAddRequestDto
     */
    @Override
    public void addOilConsumptionEquipment(TOilConsumptionEquipmentAddRequestDto tOilConsumptionEquipmentAddRequestDto) {
        if (!ObjectUtils.isEmpty(tOilConsumptionEquipmentAddRequestDto)) {
            TOilConsumptionEquipment tOilConsumptionEquipment = BeanUtil.copyProperties(tOilConsumptionEquipmentAddRequestDto, TOilConsumptionEquipment.class);
            tOilConsumptionEquipment.setMainTankSimulationAmount(JsonTool.beanToJson(tOilConsumptionEquipmentAddRequestDto.getMainTankSimulationAmount()));
            tOilConsumptionEquipment.setAuxiliaryAnalogQuantity(JsonTool.beanToJson(tOilConsumptionEquipmentAddRequestDto.getAuxiliaryAnalogQuantity()));
            this.baseMapper.insert(tOilConsumptionEquipment);
        }
    }

    /**
     * 功能描述: 修改油耗设置信息
     * @author huipanxing huipanxing@chinasofti.com
     * @date 2021/5/13 16:18
     * @param tOilConsumptionEquipmentAddRequestDto
     * @return void
     */
    @Override
    public void updateOilConsumptionEquipment(TOilConsumptionEquipmentAddRequestDto tOilConsumptionEquipmentAddRequestDto) {
        if (!ObjectUtils.isEmpty(tOilConsumptionEquipmentAddRequestDto)) {
            TOilConsumptionEquipment tOilConsumptionEquipment = BeanUtil.copyProperties(tOilConsumptionEquipmentAddRequestDto, TOilConsumptionEquipment.class);
            tOilConsumptionEquipment.setMainTankSimulationAmount(JsonTool.beanToJson(tOilConsumptionEquipmentAddRequestDto.getMainTankSimulationAmount()));
            tOilConsumptionEquipment.setAuxiliaryAnalogQuantity(JsonTool.beanToJson(tOilConsumptionEquipmentAddRequestDto.getAuxiliaryAnalogQuantity()));
            this.baseMapper.updateById(tOilConsumptionEquipment);
        }
    }

    /**
     * 根据车辆编码删除油耗设置信息
     * @param vehicleCode
     */
    @Override
    public void deleteOilConsumptionEquipment(String vehicleCode) {
        this.baseMapper.delete(new QueryWrapper<TOilConsumptionEquipment>().lambda().eq(TOilConsumptionEquipment::getVehicleCode,vehicleCode));
    }

    /**
     * 根据车辆编码查询油耗设置信息
     * @param vehicleCode
     * @return
     */
    @Override
    public TOilConsumptionEquipmentAddRequestDto selectOilConsumptionEquipment(String vehicleCode) {
        TOilConsumptionEquipment tOilConsumptionEquipment = this.baseMapper.selectOne(new QueryWrapper<TOilConsumptionEquipment>().lambda().eq(TOilConsumptionEquipment::getVehicleCode, vehicleCode));
        TOilConsumptionEquipmentAddRequestDto tOilConsumptionEquipmentAddRequestDto = BeanUtil.copyProperties(tOilConsumptionEquipment, TOilConsumptionEquipmentAddRequestDto.class);
        if (!ObjectUtils.isEmpty(tOilConsumptionEquipment.getMainTankSimulationAmount())) {
            List<String> mainTankSimulationAmountList = JSON.parseArray(tOilConsumptionEquipment.getMainTankSimulationAmount().toString(), String.class);
            tOilConsumptionEquipmentAddRequestDto.setMainTankSimulationAmount(mainTankSimulationAmountList);
        }
        if (!ObjectUtils.isEmpty(tOilConsumptionEquipment.getAuxiliaryAnalogQuantity())) {
            List<String> auxiliaryAnalogQuantityList = JSON.parseArray(tOilConsumptionEquipment.getAuxiliaryAnalogQuantity().toString(), String.class);
            tOilConsumptionEquipmentAddRequestDto.setAuxiliaryAnalogQuantity(auxiliaryAnalogQuantityList);
        }
        return tOilConsumptionEquipmentAddRequestDto;
    }
}
